/*
 *
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License version 2 as 
 * published by the Free Software Foundation.
 *
 * list.h is for what ...
 *
 * Version: ***: list.h  Sat Jul  2 11:03:46 2011 poponion Exp $
 *
 * Authors:
 *   Author poponion <huangcm85@gmail.com>
 *     -some work detail if you want 
 *
 */
#ifndef LIST_H_
#define LIST_H_
#include <stdlib.h>
#include <unistd.h>
template<class T>
class Node
{
public:
  Node();
  Node(T &data);
  
public:
  T data_;
  Node<T>* prev_;
  Node<T>* next_;
  
};
template<class T>
Node<T>::Node(T &data)
{
  prev_ = NULL;
  next_ = NULL;
  data_ = data;
}
template<class T>
class List
{
public:
  List();
  void push(T& node);
  Node<T>* pop();

  int length() const
  {
    int len = 0;
    if (NULL != head_)
    {
      len = tail_ - head_ + 1;
    }
    return len;
  }

  Node<T>* head() const
  {
    return head_;
  }

  Node<T>* tail() const
  {
    return tail_;
  }
  
private:
  Node<T>* head_;
  Node<T>* tail_;
};

template<class T>
List<T>::List()
{
  tail_ = NULL;
  head_ = NULL;
}
template<class T>
void List<T>::push(T& data)
{
  Node<T> *node = new Node<T>(data);
  if (NULL == tail_)
  {
    tail_ = node;
    head_ = tail_;
  }
  else
  {
    node->next_  = tail_->next_;
    tail_->next_ = node;
    tail_ = node;
  }
}
  
template<class T>
Node<T>* List<T>::pop()
{
  Node<T> *node = NULL;
  if (NULL != head_)
  {
    node = head_;
    if (head_ == tail_)
    {
      head_ = NULL;
      tail_ = head_;
    }
    else
    {
      head_ = head_->next_;
    }
  }
  return node;
}

#endif
